﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XuanFe.AppLaunch.Common;
using XuanFe.Database.DAL;
using XuanFe.Database.Model;

namespace XuanFe.AppLaunch
{
    public partial class FlightDetailForm : DevExpress.XtraEditors.XtraForm
    {
        private DataTable dtAirline;
        private DataTable dtCity;

        public FlightDetailForm(DataTable dtAirline, DataTable dtCity)
        {
            InitializeComponent();

            this.dtAirline = dtAirline;
            this.dtCity = dtCity;
        }
        public BookingFlightModel Data { get; private set; }
        private void BuildAll()
        {
            int visibleIndex = 0;

            this.sluAirlineId.Properties.View.Columns.Clear();
            this.sluAirlineId.Properties.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "AIRLINE_ID", Caption = "代码", Visible = true, VisibleIndex = visibleIndex++ });
            this.sluAirlineId.Properties.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "NATION_NAME", Caption = "国家", Visible = true, VisibleIndex = visibleIndex++ });
            this.sluAirlineId.Properties.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "AIRLINE_NAME", Caption = "航空公司", Visible = true, VisibleIndex = visibleIndex++ });
            this.sluAirlineId.Properties.DisplayMember = "AIRLINE_NAME";
            this.sluAirlineId.Properties.ValueMember = "AIRLINE_ID";
            this.sluAirlineId.Properties.DataSource = this.dtAirline;

            visibleIndex = 0;
            this.sluDepartureCityId.Properties.View.Columns.Clear();
            this.sluDepartureCityId.Properties.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "CITY_ID", Caption = "代码", Visible = true, VisibleIndex = visibleIndex++ });
            this.sluDepartureCityId.Properties.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "NATION_NAME", Caption = "国家", Visible = true, VisibleIndex = visibleIndex++ });
            this.sluDepartureCityId.Properties.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "CITY_NAME", Caption = "城市", Visible = true, VisibleIndex = visibleIndex++ });
            this.sluDepartureCityId.Properties.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "AIRPORT_NAME", Caption = "机场", Visible = true, VisibleIndex = visibleIndex++ });
            this.sluDepartureCityId.Properties.DisplayMember = "CITY_NAME";
            this.sluDepartureCityId.Properties.ValueMember = "CITY_ID";
            this.sluDepartureCityId.Properties.DataSource = this.dtCity;

            visibleIndex = 0;
            this.sluArrivalCityId.Properties.View.Columns.Clear();
            this.sluArrivalCityId.Properties.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "CITY_ID", Caption = "代码", Visible = true, VisibleIndex = visibleIndex++ });
            this.sluArrivalCityId.Properties.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "NATION_NAME", Caption = "国家", Visible = true, VisibleIndex = visibleIndex++ });
            this.sluArrivalCityId.Properties.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "CITY_NAME", Caption = "城市", Visible = true, VisibleIndex = visibleIndex++ });
            this.sluArrivalCityId.Properties.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "AIRPORT_NAME", Caption = "机场", Visible = true, VisibleIndex = visibleIndex++ });
            this.sluArrivalCityId.Properties.DisplayMember = "CITY_NAME";
            this.sluArrivalCityId.Properties.ValueMember = "CITY_ID";
            this.sluArrivalCityId.Properties.DataSource = this.dtCity;
        }
        private bool ValidityCheck()
        {
            this.errorProvider1.ClearErrors();
            string message = string.Empty;
            if (Global.Instance.MandatoryFieldEmpty(this.sluAirlineId, false, ref message))
            {
                this.errorProvider1.SetError(this.sluAirlineId, message);
                this.sluAirlineId.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.txtFlightNo, false, ref message))
            {
                this.errorProvider1.SetError(this.txtFlightNo, message);
                this.txtFlightNo.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.sluDepartureCityId, false, ref message))
            {
                this.errorProvider1.SetError(this.sluDepartureCityId, message);
                this.sluDepartureCityId.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.sluArrivalCityId, false, ref message))
            {
                this.errorProvider1.SetError(this.sluArrivalCityId, message);
                this.sluArrivalCityId.Focus();
                return (false);
            }

            return (true);
        }

        private void FlightDetailForm_Load(object sender, EventArgs e)
        {
            try
            {
                this.deDepartureTime.DateTime = DateTime.Now.Date;
                this.deArrivalTime.DateTime = DateTime.Now.Date;
                this.BuildAll();
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.ValidityCheck()) return;

                this.Data = new BookingFlightModel() { Id = Guid.NewGuid().ToString("N").ToUpper(), AirlineId = Convert.ToString(this.sluAirlineId.EditValue), FlightNo = this.txtFlightNo.Text, DepartureTime = this.deDepartureTime.DateTime, ArrivalTime = this.deArrivalTime.DateTime, DepartureCityId = Convert.ToString(this.sluDepartureCityId.EditValue), ArrivalCityId = Convert.ToString(this.sluArrivalCityId.EditValue) };

                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.DialogResult = DialogResult.Cancel;
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void sluAirlineId_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                string c = Convert.ToString(this.sluAirlineId.EditValue);
                string no = this.txtFlightNo.Text;
                this.txtFlightNo.Text = string.Concat(c, no);
                this.txtFlightNo.Focus();
                this.txtFlightNo.Select(2, no.Length);
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }
    }
}
